package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.ElementPortMtnFg;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.ElementSrSticknode;
import com.ruoyi.net.service.IElementSrSticknodeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-SR-粘连节点Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-SR-粘连节点")
@RestController
@RequestMapping("/element/sr/sticknode")
public class ElementSrSticknodeController extends BaseController
{
    @Autowired
    private IElementSrSticknodeService elementSrSticknodeService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 读取网元-SR-粘连节点列表
     */
    @ApiOperation(value = "读取网元-SR-粘连节点列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:list')")
    @GetMapping("/read")
    public TableDataInfo read(ElementSrSticknode elementSrSticknode)
    {
        startPage();
        List<ElementSrSticknode> list = new ArrayList<>();
        return getDIYPageTable(list);
    }

    /**
     * 查询网元-SR-粘连节点列表
     */
    @ApiOperation(value = "查询网元-SR-粘连节点列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementSrSticknode elementSrSticknode)
    {
        startPage();
        List<ElementSrSticknode> list = elementSrSticknodeService.selectElementSrSticknodeList(elementSrSticknode);
        return getDataTable(list);
    }

    /**
     * 导出网元-SR-粘连节点列表
     */
    @ApiOperation(value = "导出网元-SR-粘连节点列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:export')")
    @Log(title = "网元-SR-粘连节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementSrSticknode elementSrSticknode)
    {
        List<ElementSrSticknode> list = elementSrSticknodeService.selectElementSrSticknodeList(elementSrSticknode);
        ExcelUtil<ElementSrSticknode> util = new ExcelUtil<ElementSrSticknode>(ElementSrSticknode.class);
        util.exportExcel(response, list, "网元-SR-粘连节点数据");
    }

    /**
     * 获取网元-SR-粘连节点详细信息
     */
    @ApiOperation(value = "获取网元-SR-粘连节点详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(elementSrSticknodeService.selectElementSrSticknodeById(id));
    }

    /**
     * 新增网元-SR-粘连节点
     */
    @ApiOperation(value = "新增网元-SR-粘连节点", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:add')")
    @Log(title = "网元-SR-粘连节点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementSrSticknode elementSrSticknode)
    {
        return toAjax(elementSrSticknodeService.insertElementSrSticknode(elementSrSticknode));
    }

    /**
     * 修改网元-SR-粘连节点
     */
    @ApiOperation(value = "修改网元-SR-粘连节点", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:edit')")
    @Log(title = "网元-SR-粘连节点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementSrSticknode elementSrSticknode)
    {
        return toAjax(elementSrSticknodeService.updateElementSrSticknode(elementSrSticknode));
    }

    /**
     * 删除网元-SR-粘连节点
     */
    @ApiOperation(value = "删除网元-SR-粘连节点", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:sticknode:remove')")
    @Log(title = "网元-SR-粘连节点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(elementSrSticknodeService.deleteElementSrSticknodeByIds(ids));
    }

    // TODO: 2024/12/3  未找到SR-粘连节点对应的设备交互日志

    /**
     * 激活设备
     *
     * @param id
     */
    @ApiOperation(value = "激活设备", httpMethod = "GET")
    @GetMapping(value = "/activate/{id}")
    @Log(title = "激活设备", businessType = BusinessType.UPDATE)
    public AjaxResult activateDevice(@PathVariable("id") String id) {
        ElementSrSticknode elementSrSticknode = elementSrSticknodeService.selectElementSrSticknodeById(id);
        elementSrSticknode.setStatus("1");
        if (sendXml(elementSrSticknode)) {
            return toAjax(elementSrSticknodeService.updateElementSrSticknode(elementSrSticknode));
        } else {
            return AjaxResult.error("激活设备失败");
        }
    }

    /**
     * 去激活设备
     *
     * @param id
     */
    @ApiOperation(value = "去激活设备", httpMethod = "GET")
    @Log(title = "去激活设备", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/unactivate/{id}")
    public AjaxResult unactivateDevice(@PathVariable("id") String id) {
        ElementSrSticknode elementSrSticknode = elementSrSticknodeService.selectElementSrSticknodeById(id);
        elementSrSticknode.setStatus("0");
        if (sendXml(elementSrSticknode)) {
            return toAjax(elementSrSticknodeService.updateElementSrSticknode(elementSrSticknode));
        } else {
            return AjaxResult.error("去激活设备失败");
        }
    }

    /**
     * 发送xml到设备
     *
     * @param entity
     */
    private boolean sendXml(ElementSrSticknode entity) {
        // TODO: 2024/12/21 待与设备联调
        boolean isSend = true;
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());
            /*String xml = "<if xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-common\">" +
                    "  <interfaces>" +
                    "   <interface>" +
                    "    <name>loopback" + entity.getLoopbackId() + "</name>" +
                    "    <type>LOOPBACK</type>" +
                    "    <enabled>enable</enabled>" +
                    "    <ip xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-l2l3common\">" +
                    "     <ipv4-address-list>" +
                    "      <ipv4-address>" + entity.getIp() + "/" + entity.getMask() + "</ipv4-address>" +
                    "      <secondry-use>mast</secondry-use>" +
                    "     </ipv4-address-list>" +
                    "    </ip>" +
                    "   </interface>" +
                    "  </interfaces>" +
                    " </if>";
            System.out.println(xml);
            isSend = deviceManager.editConfig(xml, "merge");*/
        }
        return isSend;
    }
}
